home *** CD-ROM | disk | FTP | other *** search
- package javax.swing.undo;
-
- import java.util.Enumeration;
- import java.util.Vector;
- import javax.swing.event.UndoableEditEvent;
- import javax.swing.event.UndoableEditListener;
-
- public class UndoManager extends CompoundEdit implements UndoableEditListener {
- int indexOfNextAdd = 0;
- int limit = 100;
-
- public UndoManager() {
- super.edits.ensureCapacity(this.limit);
- }
-
- public synchronized boolean addEdit(UndoableEdit var1) {
- this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
- boolean var2 = super.addEdit(var1);
- if (super.inProgress) {
- var2 = true;
- }
-
- this.indexOfNextAdd = super.edits.size();
- this.trimForLimit();
- return var2;
- }
-
- public synchronized boolean canRedo() {
- if (!super.inProgress) {
- return super.canRedo();
- } else {
- UndoableEdit var1 = this.editToBeRedone();
- return var1 != null && var1.canRedo();
- }
- }
-
- public synchronized boolean canUndo() {
- if (!super.inProgress) {
- return super.canUndo();
- } else {
- UndoableEdit var1 = this.editToBeUndone();
- return var1 != null && var1.canUndo();
- }
- }
-
- public synchronized boolean canUndoOrRedo() {
- return this.indexOfNextAdd == super.edits.size() ? this.canUndo() : this.canRedo();
- }
-
- public synchronized void discardAllEdits() {
- Enumeration var1 = super.edits.elements();
-
- while(var1.hasMoreElements()) {
- UndoableEdit var2 = (UndoableEdit)var1.nextElement();
- var2.die();
- }
-
- super.edits = new Vector(this.limit);
- this.indexOfNextAdd = 0;
- }
-
- protected UndoableEdit editToBeRedone() {
- int var1 = super.edits.size();
- int var2 = this.indexOfNextAdd;
-
- while(var2 < var1) {
- UndoableEdit var3 = (UndoableEdit)super.edits.elementAt(var2++);
- if (var3.isSignificant()) {
- return var3;
- }
- }
-
- return null;
- }
-
- protected UndoableEdit editToBeUndone() {
- int var1 = this.indexOfNextAdd;
-
- while(var1 > 0) {
- --var1;
- UndoableEdit var2 = (UndoableEdit)super.edits.elementAt(var1);
- if (var2.isSignificant()) {
- return var2;
- }
- }
-
- return null;
- }
-
- public synchronized void end() {
- super.end();
- this.trimEdits(this.indexOfNextAdd, super.edits.size() - 1);
- }
-
- public synchronized int getLimit() {
- return this.limit;
- }
-
- public synchronized String getRedoPresentationName() {
- if (super.inProgress) {
- return this.canRedo() ? this.editToBeRedone().getRedoPresentationName() : "Redo";
- } else {
- return super.getRedoPresentationName();
- }
- }
-
- public synchronized String getUndoOrRedoPresentationName() {
- return this.indexOfNextAdd == super.edits.size() ? this.getUndoPresentationName() : this.getRedoPresentationName();
- }
-
- public synchronized String getUndoPresentationName() {
- if (super.inProgress) {
- return this.canUndo() ? this.editToBeUndone().getUndoPresentationName() : "Undo";
- } else {
- return super.getUndoPresentationName();
- }
- }
-
- public synchronized void redo() throws CannotRedoException {
- if (super.inProgress) {
- UndoableEdit var1 = this.editToBeRedone();
- if (var1 == null) {
- throw new CannotRedoException();
- }
-
- this.redoTo(var1);
- } else {
- super.redo();
- }
-
- }
-
- protected void redoTo(UndoableEdit var1) throws CannotRedoException {
- UndoableEdit var3;
- for(boolean var2 = false; !var2; var2 = var3 == var1) {
- var3 = (UndoableEdit)super.edits.elementAt(this.indexOfNextAdd++);
- var3.redo();
- }
-
- }
-
- public synchronized void setLimit(int var1) {
- this.limit = var1;
- this.trimForLimit();
- }
-
- public String toString() {
- return super.toString() + " limit: " + this.limit + " indexOfNextAdd: " + this.indexOfNextAdd;
- }
-
- protected void trimEdits(int var1, int var2) {
- if (var1 <= var2) {
- for(int var3 = var2; var1 <= var3; --var3) {
- UndoableEdit var4 = (UndoableEdit)super.edits.elementAt(var3);
- var4.die();
- super.edits.removeElementAt(var3);
- }
-
- if (this.indexOfNextAdd > var2) {
- this.indexOfNextAdd -= var2 - var1 + 1;
- } else if (this.indexOfNextAdd >= var1) {
- this.indexOfNextAdd = var1;
- }
- }
-
- }
-
- protected void trimForLimit() {
- if (this.limit > 0) {
- int var1 = super.edits.size();
- if (var1 > this.limit) {
- int var2 = this.limit / 2;
- int var3 = this.indexOfNextAdd - 1 - var2;
- int var4 = this.indexOfNextAdd - 1 + var2;
- if (var4 - var3 + 1 > this.limit) {
- ++var3;
- }
-
- if (var3 < 0) {
- var4 -= var3;
- var3 = 0;
- }
-
- if (var4 >= var1) {
- int var5 = var1 - var4 - 1;
- var4 += var5;
- var3 += var5;
- }
-
- this.trimEdits(var4 + 1, var1 - 1);
- this.trimEdits(0, var3 - 1);
- }
- }
-
- }
-
- public synchronized void undo() throws CannotUndoException {
- if (super.inProgress) {
- UndoableEdit var1 = this.editToBeUndone();
- if (var1 == null) {
- throw new CannotUndoException();
- }
-
- this.undoTo(var1);
- } else {
- super.undo();
- }
-
- }
-
- public synchronized void undoOrRedo() throws CannotRedoException, CannotUndoException {
- if (this.indexOfNextAdd == super.edits.size()) {
- this.undo();
- } else {
- this.redo();
- }
-
- }
-
- protected void undoTo(UndoableEdit var1) throws CannotUndoException {
- UndoableEdit var3;
- for(boolean var2 = false; !var2; var2 = var3 == var1) {
- var3 = (UndoableEdit)super.edits.elementAt(--this.indexOfNextAdd);
- var3.undo();
- }
-
- }
-
- public void undoableEditHappened(UndoableEditEvent var1) {
- this.addEdit(var1.getEdit());
- }
- }
-